package com.tarena.lbs.content.controller;

import com.alibaba.fastjson2.JSONObject;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageQuery;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.service.ArticleService;
import com.tarena.lbs.pojo.content.dto.ArticleActionPageResultDTO;
import com.tarena.lbs.pojo.content.dto.ArticleContentDTO;
import com.tarena.lbs.pojo.content.param.ArticleActionParam;
import com.tarena.lbs.pojo.content.param.ArticleContentParam;
import com.tarena.lbs.pojo.content.query.ArticleQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.HashSet;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("admin/content/article")
@Slf4j
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @PostMapping("/insert")
    public Result<Void> insert(ArticleContentParam articleContentParam) {
        log.info("ArticleController insert articleContentParam={}",JSONObject.toJSONString(articleContentParam));
        articleService.insert(articleContentParam);
        return Result.success();
    }

    @PostMapping("/delete")
    public Result<Void> delete(String id) throws BusinessException {
        articleService.delete(id);
        return Result.success();
    }

    @GetMapping("/get")
    @ApiOperation("文章详情")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id", name = "id", example = "1", required = true)
    })
    public Result<ArticleContentDTO> get(String id) throws BusinessException {
        ArticleContentDTO articleContentDTO = articleService.get(id);
        return new Result<>(articleContentDTO);
    }


    @PostMapping("/update")
    public Result<Void> update(ArticleContentParam articleContentParam) throws BusinessException {
        articleService.update(articleContentParam);
        return Result.success();
    }


    @GetMapping("/list")
    public Result<PageResult<ArticleContentDTO>> list(ArticleQuery articleQuery) {
        log.info("ArticleController list articleQuery={}", JSONObject.toJSONString(articleQuery));
        PageResult<ArticleContentDTO> list = articleService.list(articleQuery);
        return new Result<>(list);
    }

    @PostMapping("/action")
    public Result<Void> action(ArticleActionParam articleActionParam) throws BusinessException {
        articleService.action(articleActionParam);
        return Result.success();
    }

    @PostMapping("/behavior")
    public Result<Void> behavior(ArticleActionParam articleActionParam) throws BusinessException {
        articleService.behavior(articleActionParam);
        articleService.action(articleActionParam);
        return Result.success();
    }

    @GetMapping("/getBehaviorList")
    public Result<ArticleActionPageResultDTO> getCurrentUserBehavior(BasePageQuery pageQuery) {
        ArticleActionPageResultDTO articleActionPageResultDTO = articleService.getCurrentUserBehavior(pageQuery);
        return new Result<>(articleActionPageResultDTO);
    }

    @GetMapping("/getArticleLabel")
    public Result<HashSet<String>> get(ArticleQuery articleQuery) {
        HashSet<String> articleLabelSet = articleService.getArticleLabel(articleQuery);
        return new Result<>(articleLabelSet);
    }
}
